package algotithm.weekendrace.biweeklycontest51.test3;

import java.util.Arrays;

/**
 * @author 25068
 * @Date 2021/5/1 22:26
 * @Title 5732. 减小和重新排列数组后的最大元素
 */
public class Solution {


    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {2,2,1,2,1};
        int i = solution.maximumElementAfterDecrementingAndRearranging(arr);
        System.out.println(i);
    }

    public int maximumElementAfterDecrementingAndRearranging(int[] arr) {
        int ans = Integer.MIN_VALUE;
        Arrays.sort(arr);
        if (arr[0] != 1) {
            arr[0] = 1;
        }
        int n = arr.length;
        for (int i = 1; i < n; i++) {
            if (Math.abs(arr[i - 1] - arr[i]) > 1) {
                arr[i] = arr[i - 1] + 1;
            }
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] > ans) {
                ans = arr[i];
            }
        }
        return ans;
    }

}
